package controllers

import (
	"github.com/gin-gonic/gin"
	"time"
	"zbn-oil-consumption/middleware"
	"zbn-oil-consumption/models"
	"zbn-oil-consumption/utils"
)

type Admin struct {
}

var mUser models.User
var mAdmin models.Admin

type TokenUser struct {
	*models.User
	Token string `json:"token"`
}

// Login 登录
func (Admin) Login(c *gin.Context) {
	var adm models.Admin
	if err := c.ShouldBindJSON(&adm); err != nil {
		utils.Fail(c, 0, "参数错误")
		return
	}
	res, err := mAdmin.FindByUsername(adm.Username)
	if err != nil {
		utils.Fail(c, 0, "用户不存在")
		return
	}
	if res.Password != adm.Password {
		utils.Fail(c, 0, "密码错误")
		return
	}
	if res.Status != 1 {
		utils.Fail(c, 0, "用户被禁用")
		return
	}
	token, err := middleware.GenerateAdminTokenPair(&res)
	if err != nil {
		utils.Fail(c, 0, "登录失败")
		return
	}
	utils.Success(c, 200, "", token)
}

// AdminSave 添加管理员
func (Admin) AdminSave(c *gin.Context) {
	var d models.Admin
	if err := c.ShouldBindJSON(&d); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	if d.Id == 0 {
		d.LastLogin = time.Time{}
		d.CreatedAt = time.Now()
		res, err := mAdmin.Add(d)
		if err != nil {
			utils.Fail(c, 0, "")
			return
		}
		utils.Success(c, 200, "", res)
	} else {
		res, err := mAdmin.Update(d)
		if err != nil {
			utils.Fail(c, 0, "")
			return
		}
		utils.Success(c, 200, "", res)
	}
}

// AdminUpdate 修改管理员
func (Admin) AdminUpdate(c *gin.Context) {
	var d models.Admin
	if err := c.ShouldBindJSON(&d); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	d.LastLogin = time.Time{}
	d.CreatedAt = time.Now()
	res, err := mAdmin.Update(d)
	if err != nil {
		utils.Fail(c, 0, "")
		return
	}
	utils.Success(c, 200, "", res)
}

// AdminDelete 删除管理员
func (Admin) AdminDelete(c *gin.Context) {
	var d models.Admin
	if err := c.ShouldBindJSON(&d); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	res, err := mAdmin.Delete(d)
	if err != nil {
		utils.Fail(c, 0, err.Error())
		return
	}
	utils.Success(c, 200, "", res)
}

// AdminChangeStatus 修改管理员状态
func (Admin) AdminChangeStatus(c *gin.Context) {
	var d models.Admin
	if err := c.ShouldBindJSON(&d); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	res, err := mAdmin.ChangeStatus(d)
	if err != nil {
		utils.Fail(c, 0, "")
		return
	}
	utils.Success(c, 200, "", res)
}

// AdminList 获取所有管理员
func (Admin) AdminList(c *gin.Context) {
	var params utils.Params
	if err := c.ShouldBind(&params); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	total, res, err := mAdmin.GetList(params)
	if err != nil {
		utils.Fail(c, 0, err.Error())
		return
	}
	utils.SuccessPagination(c, 200, "success", res, total, params.Page, params.PageSize)
}

// UserSave 添加用户
func (Admin) UserSave(c *gin.Context) {
	var d models.User
	if err := c.ShouldBindJSON(&d); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	if d.Id == 0 {
		d.LastLoginTime = time.Time{}
		d.CreatedAt = time.Now()
		res, err := mUser.Add(d)
		if err != nil {
			utils.Fail(c, 0, "")
			return
		}
		utils.Success(c, 200, "", res)
	} else {
		res, err := mUser.Update(d)
		if err != nil {
			utils.Fail(c, 0, "")
			return
		}
		utils.Success(c, 200, "", res)
	}
}

// UserDelete 删除用户
func (Admin) UserDelete(c *gin.Context) {
	var user models.User
	if err := c.ShouldBindJSON(&user); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	res, err := mUser.Delete(user)
	if err != nil {
		utils.Fail(c, 0, err.Error())
		return
	}
	utils.Success(c, 200, "", res)
}

// UserList 获取所有用户
func (Admin) UserList(c *gin.Context) {
	var params utils.Params
	if err := c.ShouldBind(&params); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	total, res, err := mUser.GetList(params)
	if err != nil {
		utils.Fail(c, 0, err.Error())
		return
	}
	utils.SuccessPagination(c, 200, "success", res, total, params.Page, params.PageSize)
}

// UserChangeStatus 修改用户状态
func (Admin) UserChangeStatus(c *gin.Context) {
	var d models.User
	if err := c.ShouldBindJSON(&d); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	res, err := mUser.ChangeStatus(d)
	if err != nil {
		utils.Fail(c, 0, "")
		return
	}
	utils.Success(c, 200, "", res)
}

// RecordList 获取所有记录
func (Admin) RecordList(c *gin.Context) {
	var params utils.Params
	if err := c.ShouldBind(&params); err != nil {
		utils.Fail(c, 0, "无效数据")
		return
	}
	total, res, err := mRecord.GetList(params)
	if err != nil {
		utils.Fail(c, 0, err.Error())
		return
	}
	utils.SuccessPagination(c, 200, "success", res, total, params.Page, params.PageSize)
}

// Chart 获取图表数据

/*
*

	{
	    "date": "2023-01-01",
	    "count": 125,
	    "prevCount": 120,
	    "typeDistribution": [
	      { "name": "类型A", "value": 35 },
	      { "name": "类型B", "value": 25 }
	    ],
	    "comparisonData": [
	      { "name": "上周", "value": 120 },
	      { "name": "本周", "value": 150 }
	    ]
	  }
*/
type KeyValue struct {
	Name  string `json:"name"`
	Value int    `json:"value"`
}
type ChartData struct {
	Date             string     `json:"date"`
	Count            int        `json:"count"`
	PrevCount        int        `json:"prevCount"`
	TypeDistribution []KeyValue `json:"typeDistribution"`
	ComparisonData   []KeyValue `json:"comparisonData"`
}

func (Admin) Chart(c *gin.Context) {
	var t []KeyValue
	var week []KeyValue
	t = append(t, KeyValue{
		Name:  "接龙",
		Value: 134,
	})
	t = append(t, KeyValue{
		Name:  "得分",
		Value: 534,
	})
	week = append(week, KeyValue{
		Name:  "上周",
		Value: 355,
	})
	week = append(week, KeyValue{
		Name:  "本周",
		Value: 555,
	})
	var data []ChartData
	res := ChartData{
		Date:             "2025-06-06",
		Count:            323,
		PrevCount:        353,
		TypeDistribution: t,
		ComparisonData:   week,
	}
	data = append(data, res)
	utils.Success(c, 200, "success", data)
}
